clc
clear
close all

%%
rolloff = 0.35;
span= 4;
sps = 4;

SampleRate = 200e3;

txfilter = comm.RaisedCosineTransmitFilter( ...
    RolloffFactor=rolloff, ...
    FilterSpanInSymbols=span, ...
    OutputSamplesPerSymbol=sps);

rxfilter = comm.RaisedCosineReceiveFilter( ...
    RolloffFactor=rolloff, ...
    FilterSpanInSymbols=span, ...
    InputSamplesPerSymbol=sps, ...
    DecimationFactor=sps);

M = [4 12];
radii = [1 2];
modOrder = sum(M);
bps = log2(modOrder);
filtDelay = span;
errorRate = comm.ErrorRate(ReceiveDelay=filtDelay);
errorRate1 = comm.ErrorRate(ReceiveDelay=filtDelay);

x = randi([0 modOrder-1],1000,1);
modSig = apskmod(x,M,radii);
z0 = apskdemod(modSig,M,radii);
txSig = txfilter(modSig);
bw = obw(txSig, SampleRate);
bw = fix(bw/1000)*1000;
y = lowpass(txSig, SampleRate/sps, SampleRate, ImpulseResponse="fir", Steepness=0.99);

rxSig = rxfilter(txSig);
rxSig1 = rxfilter(y);
z = apskdemod(rxSig,M,radii);
z1 = apskdemod(rxSig1,M,radii);
errStat = errorRate(x,z);
errStat1 = errorRate1(x,z1);

%%
TimeDuration = 0.1;
SampleRate = 200e3;
ModulatorOrder = [4 8 20];
MasterClockRate = 600e3;
SamplePerSymbol = 4;

CarrierFrequency = 200e3;
IqImbalanceConfig.A = 3;
IqImbalanceConfig.P = 10;
PhaseNoiseConfig.Level = -50;
PhaseNoiseConfig.FrequencyOffset = 20;
MemoryLessNonlinearityConfig.Method = 'Saleh model';
MemoryLessNonlinearityConfig.InputScaling = 0;
MemoryLessNonlinearityConfig.AMAMParameters = [2.1587 1.1517];
MemoryLessNonlinearityConfig.AMPMParameters =  [4.0033 9.1040];
MemoryLessNonlinearityConfig.OutputScaling = 0;
MemoryLessNonlinearityConfig.ReferenceImpedance = 1;
ThermalNoiseConfig.NoiseTemperature = 290;
AGCConfig.AveragingLength = 256 * 4;
AGCConfig.MaxPowerGain = 400;

ModulatorConfig.Radii = [0.3 0.7 1.2];
ModulatorConfig.PhaseOffset = [0 0 0];
ModulatorConfig.beta = 0.35;
ModulatorConfig.span = 4;

source = RandomSource(SampleRate=SampleRate, ...
    TimeDuration=TimeDuration, ...
    ModulatorOrder=ModulatorOrder, ...
    SamplePerSymbol=SamplePerSymbol);

x = source();

%% 单天线场景

txRF = TRFSimulator(StartTime=0.2, ...
    SampleRate = SampleRate, ...
    CarrierFrequency=CarrierFrequency, ...
    IqImbalanceConfig=IqImbalanceConfig, ...
    PhaseNoiseConfig=PhaseNoiseConfig, ...
    MasterClockRate = MasterClockRate, ...
    MemoryLessNonlinearityConfig=MemoryLessNonlinearityConfig);
rayChannel = Rayleigh(CarrierFrequency=CarrierFrequency, ...
    SampleRate=SampleRate, PathDelays=0, ...
    AveragePathGains=0, MaximumDopplerShift=0);

ricChannel = Rician(CarrierFrequency=CarrierFrequency, ...
    SampleRate=SampleRate, PathDelays=0, ...
    AveragePathGains=0, MaximumDopplerShift=0);

rxRF = RRFSimulator(StartTime=0, TimeDuration=2, SampleRate=SampleRate, ...
    NumReceiveAntennas=1, CarrierFrequency=200e3, Bandwidth=20e3, ...
    MasterClockRate=MasterClockRate, ...
    MemoryLessNonlinearityConfig=MemoryLessNonlinearityConfig, ...
    ThermalNoiseConfig=ThermalNoiseConfig, ...
    PhaseNoiseConfig=PhaseNoiseConfig, ...
    AGCConfig=AGCConfig, ...
    IqImbalanceConfig=IqImbalanceConfig);

baseBandSignal = APSK(SampleRate=SampleRate, ...
    TimeDuration=TimeDuration, ...
    ModulatorOrder=ModulatorOrder, ...
    SamplePerSymbol=SamplePerSymbol, ...
    ModulatorConfig=ModulatorConfig);

x1= baseBandSignal(x);
x1 = txRF(x1);
x1 = rayChannel(x1);
y1 = rxRF(x1);

%% 多天线场景
NumTransmitAntennas = 2;
NumReceiveAntennas = 1;

txRF = TRFSimulator(StartTime=0.2, ...
    SampleRate = SampleRate, ...
    CarrierFrequency=CarrierFrequency, ...
    IqImbalanceConfig=IqImbalanceConfig, ...
    PhaseNoiseConfig=PhaseNoiseConfig, ...
    MasterClockRate = MasterClockRate, ...
    MemoryLessNonlinearityConfig=MemoryLessNonlinearityConfig);
mimoChannel = MIMO(CarrierFrequency=CarrierFrequency, ...
    SampleRate=SampleRate, PathDelays=0, ...
    AveragePathGains=0, MaximumDopplerShift=0, ...
    NumTransmitAntennas=NumTransmitAntennas, ...
    NumReceiveAntennas=NumReceiveAntennas);
rxRF = RRFSimulator(StartTime=0, TimeDuration=2, SampleRate=SampleRate, ...
    NumReceiveAntennas=NumReceiveAntennas, CarrierFrequency=200e3, Bandwidth=20e3, ...
    MasterClockRate=MasterClockRate, ...
    MemoryLessNonlinearityConfig=MemoryLessNonlinearityConfig, ...
    ThermalNoiseConfig=ThermalNoiseConfig, ...
    PhaseNoiseConfig=PhaseNoiseConfig, ...
    AGCConfig=AGCConfig, ...
    IqImbalanceConfig=IqImbalanceConfig);

baseBandSignal = APSK(SampleRate=SampleRate, ...
    TimeDuration=TimeDuration, ...
    ModulatorOrder=ModulatorOrder, ...
    SamplePerSymbol=SamplePerSymbol, ...
    NumTransmitAntennas = NumTransmitAntennas, ...
    ModulatorConfig=ModulatorConfig);

x2 = baseBandSignal(x);
x2 = txRF(x2);
x2 = mimoChannel(x2);
y2 = rxRF({x2});

NumTransmitAntennas = 2;
NumReceiveAntennas = 2;

txRF = TRFSimulator(StartTime=0.2, ...
    SampleRate = SampleRate, ...
    CarrierFrequency=CarrierFrequency, ...
    IqImbalanceConfig=IqImbalanceConfig, ...
    PhaseNoiseConfig=PhaseNoiseConfig, ...
    MasterClockRate = MasterClockRate, ...
    MemoryLessNonlinearityConfig=MemoryLessNonlinearityConfig);
mimoChannel = MIMO(CarrierFrequency=CarrierFrequency, ...
    SampleRate=SampleRate, PathDelays=0, ...
    AveragePathGains=0, MaximumDopplerShift=0, ...
    NumTransmitAntennas=NumTransmitAntennas, ...
    NumReceiveAntennas=NumReceiveAntennas);
rxRF = RRFSimulator(StartTime=0, TimeDuration=2, SampleRate=SampleRate, ...
    NumReceiveAntennas=NumReceiveAntennas, CarrierFrequency=200e3, Bandwidth=20e3, ...
    MasterClockRate=MasterClockRate, ...
    MemoryLessNonlinearityConfig=MemoryLessNonlinearityConfig, ...
    ThermalNoiseConfig=ThermalNoiseConfig, ...
    PhaseNoiseConfig=PhaseNoiseConfig, ...
    AGCConfig=AGCConfig, ...
    IqImbalanceConfig=IqImbalanceConfig);

baseBandSignal = APSK(SampleRate=SampleRate, ...
    TimeDuration=TimeDuration, ...
    ModulatorOrder=ModulatorOrder, ...
    SamplePerSymbol=SamplePerSymbol, ...
    NumTransmitAntennas = NumTransmitAntennas, ...
    ModulatorConfig=ModulatorConfig);

x3 = baseBandSignal(x);
x3 = txRF(x3);
x3 = mimoChannel(x3);
y3 = rxRF({x3});

NumTransmitAntennas = 1;
NumReceiveAntennas = 2;

txRF = TRFSimulator(StartTime=0.2, ...
    SampleRate = SampleRate, ...
    CarrierFrequency=CarrierFrequency, ...
    IqImbalanceConfig=IqImbalanceConfig, ...
    PhaseNoiseConfig=PhaseNoiseConfig, ...
    MasterClockRate = MasterClockRate, ...
    MemoryLessNonlinearityConfig=MemoryLessNonlinearityConfig);
mimoChannel = MIMO(CarrierFrequency=CarrierFrequency, ...
    SampleRate=SampleRate, PathDelays=0, ...
    AveragePathGains=0, MaximumDopplerShift=0, ...
    NumTransmitAntennas=NumTransmitAntennas, ...
    NumReceiveAntennas=NumReceiveAntennas);
rxRF = RRFSimulator(StartTime=0, TimeDuration=2, SampleRate=SampleRate, ...
    NumReceiveAntennas=NumReceiveAntennas, CarrierFrequency=200e3, Bandwidth=20e3, ...
    MasterClockRate=MasterClockRate, ...
    MemoryLessNonlinearityConfig=MemoryLessNonlinearityConfig, ...
    ThermalNoiseConfig=ThermalNoiseConfig, ...
    PhaseNoiseConfig=PhaseNoiseConfig, ...
    AGCConfig=AGCConfig, ...
    IqImbalanceConfig=IqImbalanceConfig);

baseBandSignal = APSK(SampleRate=SampleRate, ...
    TimeDuration=TimeDuration, ...
    ModulatorOrder=ModulatorOrder, ...
    SamplePerSymbol=SamplePerSymbol, ...
    NumTransmitAntennas = NumTransmitAntennas, ...
    ModulatorConfig=ModulatorConfig);

x4 = baseBandSignal(x);
x4 = txRF(x4);
x4 = mimoChannel(x4);
y4 = rxRF({x4});